package com.example.express.controller;

import com.example.express.bean.Message;
import com.example.express.mvc.ResponseBody;
import com.example.express.service.BaseAdminService;
import com.example.express.service.impl.AdminServiceImpl;
import com.example.express.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {
    private BaseAdminService adminService = new AdminServiceImpl();

    @ResponseBody("/admin/login.do")
    public String Login(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean result = adminService.login(username, password);
        Message message = null;
        if (result){
            message = new Message(0,"登录成功");
            Date date = new Date();
            String ip = req.getRemoteAddr();
            adminService.updateLoginTime(username,date,ip);
            req.getSession().setAttribute("adminUserName",username);
        }else {
            message = new Message(-1,"登录失败");
        }
        return JSONUtil.toJSON(message);
    }
}
